//
//  JEHSecondTableViewCell.m
//  SinaNews
//
//  Created by 蒋恩浩 on 15/12/9.
//  Copyright © 2015年 JEH. All rights reserved.
//

#import "JEHSecondTableViewCell.h"

@interface JEHSecondTableViewCell ()

@end

@implementation JEHSecondTableViewCell

- (void)dealloc {
    
    [_leftLabel release];
    [_rightLabel release];
    [_switches release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView {
    
    self.leftLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [_leftLabel release];
    _leftLabel.textAlignment = NSTextAlignmentLeft;
    _leftLabel.font = [UIFont systemFontOfSize:15];
    self.rightLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [_rightLabel release];
    _rightLabel.textAlignment = NSTextAlignmentRight;
    _rightLabel.font = [UIFont systemFontOfSize:15];
    _rightLabel.text = @"❯";
    self.switches = [[UISwitch alloc] initWithFrame:CGRectZero];
    [_switches release];
//    [_switches addTarget:self action:@selector(switchChangeAction:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_switches];
    [self addSubview:_leftLabel];
    [self addSubview:_rightLabel];
    
#pragma mark - NightMode
    NSNotificationCenter *centers = [NSNotificationCenter defaultCenter];
    [centers addObserver:self selector:@selector(nightMode:) name:@"NightMode" object:nil];
}

- (void)nightMode:(NSNotification *)notification {
    
    if ([notification.object isEqualToString:@"YES"]) {
        
        self.leftLabel.textColor = [UIColor whiteColor];
        self.rightLabel.textColor = [UIColor whiteColor];
    } else {
        self.leftLabel.textColor = [UIColor blackColor];
        self.rightLabel.textColor = [UIColor blackColor];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat leftTitleWidth = [self getWidthWithText:self.leftLabel.text withFont:15];
    self.leftLabel.frame = CGRectMake(20, 0, leftTitleWidth + 15, self.height);
    self.rightLabel.frame = CGRectMake(self.leftLabel.width + 10, 0, self.width - 20 - leftTitleWidth - 15, self.height);
    self.switches.frame = CGRectMake(self.width - 70, self.height / 2 - 15, 60, 30);
}

- (CGFloat)getWidthWithText:(NSString *)text withFont:(CGFloat)font {
    NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:font]};
    CGRect rect = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX,0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.width;
}

//- (void)switchChangeAction:(UISwitch *)switches {
//    
//    
//}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
